package com.payment.ktb.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.payment.ktb.Fragment.Fragment_main1;
import com.payment.ktb.Fragment.Fragment_main3;
import com.payment.ktb.Fragment.Fragment_main4;
import com.payment.ktb.R;
import com.payment.ktb.activity.main2.ShopActivity;
import com.payment.ktb.constants.ConstantsUpdateArgs;
import com.payment.ktb.utils.SharedPreferencesUtils;
import com.payment.ktb.view.UpdateDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    String fragmentTag = "fragmentTag";
    FragmentManager manager;
    FragmentTransaction transaction;
    Fragment_main1 fragment_main1 = new Fragment_main1();
    Fragment_main3 fragment_main3 = new Fragment_main3();
    Fragment_main4 fragment_main4 = new Fragment_main4();
    @BindView(R.id.rg_main)
    RadioGroup rg_main;
    @BindView(R.id.rb_main1)
    RadioButton rb_main1;
    @BindView(R.id.rb_main2)
    RadioButton rb_main2;
    @BindView(R.id.rb_main3)
    RadioButton rb_main3;
    @BindView(R.id.rb_main4)
    RadioButton rb_main4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        rb_main1.setChecked(true);
        rg_main.setOnCheckedChangeListener(this);
        manager = getSupportFragmentManager();
        changeFragment(fragment_main1);
        putFragmentTag("1");
    }







    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            //首页
            case R.id.rb_main1:
                putFragmentTag("1");
                changeFragment(fragment_main1);
                break;
            //积分商城
            case R.id.rb_main2:
//                changeFragment(new Fragment_main2());

                startActivity(ShopActivity.class);

                break;
            //订单
            case R.id.rb_main3:
                putFragmentTag("3");
                changeFragment(fragment_main3);
                break;
            //个人中心
            case R.id.rb_main4:
                putFragmentTag("4");
                changeFragment(fragment_main4);
                break;
        }

    }

    public void changeFragment(Fragment fragment) {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fl_main, fragment);
        transaction.commit();

        //自动更新
        if (ConstantsUpdateArgs.isUpd.equals("1") && ((fragment instanceof Fragment_main1) || (fragment instanceof Fragment_main3))) {
            ConstantsUpdateArgs.isUpd = "0";
            UpdateDialog dialog = new UpdateDialog(mContext);
            dialog.show();
            dialog.setCanceledOnTouchOutside(false);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();

        if ("1".equals(SharedPreferencesUtils.getString(fragmentTag))) {
            rb_main1.setChecked(true);
            changeFragment(fragment_main1);
        }
        if ("3".equals(SharedPreferencesUtils.getString(fragmentTag))) {
            rb_main3.setChecked(true);
            changeFragment(fragment_main3);
        }
        if ("4".equals(SharedPreferencesUtils.getString(fragmentTag))) {
            rb_main4.setChecked(true);
            changeFragment(fragment_main4);
        }

    }



    public void putFragmentTag(String fragmentIndex) {
        SharedPreferencesUtils.putString(fragmentTag,fragmentIndex);
    }
}
